package org.txazo.blog.commons.search;

import java.io.File;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

public class Indexer {

	public static void main(String[] args) throws Exception {
		String index = "D:/index";

		/** Directory */
		Directory directory = FSDirectory.open(new File(index));

		/** Analyzer */
		Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
		IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44,
				analyzer);

		/** IndexWriter */
		IndexWriter writer = new IndexWriter(directory, config);

		/** Document */
		Document document = new Document();

		/** Field */
		document.add(new StringField("id", "1", Store.YES));
		document.add(new TextField("name", "几多愁", Store.YES));
		document.add(new TextField("content", "命运总是颠沛流离", Store.YES));

		writer.addDocument(document);

		writer.commit();

		writer.close();
	}
}
